﻿using lBll;
using lDal;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class KliporBll : lKliporBll
    {
        public KliporBll(lCaseDal<Klipoerty> dal)
        {
            Dal = dal;
        }

        public lCaseDal<Klipoerty> Dal { get; }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="klipoerty">表名</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> KAdd(Klipoerty klipoerty)
        {
            klipoerty.IsDeleted = true;
            return Dal.AddAsync(klipoerty);
        }

        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="name">项目名称模糊查询</param>
        /// <param name="index">页数</param>
        /// <param name="size">条数</param>
        /// <returns>通过条件查询进行分页和显示</returns>
        public dynamic KShow(string? name, int index, int size)
        {
            var m=Dal.GetList().Where(x=>x.IsDeleted==true);
            var query = from a in m
                        select a;
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(x => x.Kname.Contains(name));
            }
            var count=query.Count();
            var list = query.OrderBy(x => x.Id).Skip((index - 1) * size).Take(size);
            return new
            {
                count,
                list
            };
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>通过主键id进行逻辑删除</returns>
        public Task<int> Delete(int id)
        {
            var m = Dal.GetList().FirstOrDefault(x => x.Id == id);
            m.IsDeleted = false;
            return Dal.UpdateAsync(m);
        }

        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>通过主键id进行批量修改</returns>
        public Task<int> UpdState(string idstring)
        {
            List<int> ids=idstring.Split(',').Select(x=>Convert.ToInt32(x)).ToList();
            var Zt=Dal.GetList().Where(x=>ids.Contains(x.Id)).ToList();
            foreach (var tr in Zt)
            {
                if (tr.Zt == 1)
                {
                    tr.Zt = 2;
                }
                else
                {
                    tr.Zt = 1;
                }
            }
            return Dal.UpdateRangeAsync(Zt);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <returns>返回受影响行数</returns>
        public List<Klipoerty> DaoChu()
        {
            return Dal.GetList().ToList();
        }
    }
}
